package cn.kgc.countservice.controller;

import cn.kgc.countservice.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
public class AddressController {
    @Autowired
    private AddressService addressService;

    @GetMapping("/count")
    public Integer addOneAddress(@RequestParam("newsId")Integer newsId,
                                 @RequestParam("address") String address){
        return addressService.addOneAddress(newsId, address);
    }

    @GetMapping("/count/{newsId}")
    public Map<Integer, Integer> getAllCounts(@PathVariable("newsId") String newsList){
        List<Integer> list = new ArrayList<>();
        String[] array = newsList.split(",");
        for (String s : array) {
            list.add(Integer.parseInt(s));
        }
        return addressService.getAllCounts(list);
    }

    @GetMapping("/hot")
    public List<Integer> getHotNews(@RequestParam("newsIdList") Integer[] newsIdList){
        return addressService.getHotNews(Arrays.asList(newsIdList));
    }


    @GetMapping("/rmzSet")
    public long zSetRemove(@RequestParam("key") String key){
        System.out.println("count-service:"+key);
        return addressService.zSetRemove(key, 0, 10);
    }

    @PostMapping("/setHotNews")
    public void setHot(@RequestParam("key") String key, @RequestParam("value") String value){
        addressService.setHot(key, value);
    }

    @GetMapping("/getHotNews")
    public String getHot(@RequestParam("key") String key){
        return addressService.getHot(key);
    }
}
